import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_wisave/Common/TimeSwitching.dart';
import 'package:flutter_wisave/Tools/NetworkService.dart';

// class GroupRankingPage extends StatelessWidget {
//   const GroupRankingPage({super.key});

//   get child => null;

class GroupRankingPage extends StatefulWidget {
  const GroupRankingPage({super.key});

  @override
  State<GroupRankingPage> createState() => _GroupRankingPageState();
}

class _GroupRankingPageState extends State<GroupRankingPage>
    with SingleTickerProviderStateMixin {
  DateTime _currentMonthDateTime = DateTime.now();
  final NetworkService _networkService = NetworkService();
  late String currentMonthDate;
  late String startDate;
  late String endDate;

  @override
  void initState() {
    super.initState();
    _networkService.setupInterceptors(context);
    currentMonthDate = _formatMonthDate(_currentMonthDateTime);
  }

  String _formatMonthDate(DateTime date) {
    return '${date.year}-${date.month.toString().padLeft(2, '0')}';
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, // 状态栏颜色
    ));

    final mediaQuery = MediaQuery.of(context);
    final screenWidth = mediaQuery.size.width;
    final screenHeight = mediaQuery.size.height;

    void _onMonthDateChanged(DateTime newDate) {
      setState(() {
        _currentMonthDateTime = newDate;
        currentMonthDate = _formatMonthDate(_currentMonthDateTime);
        startDate =
            DateTime(_currentMonthDateTime.year, _currentMonthDateTime.month, 1)
                .toString()
                .split(' ')[0];
        endDate = DateTime(
                _currentMonthDateTime.year, _currentMonthDateTime.month + 1, 0)
            .toString()
            .split(' ')[0];
      });
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text('班组月产量排名'),
        backgroundColor: Colors.transparent,
        flexibleSpace: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/home/home_topbg.png'),
              fit: BoxFit.cover,
            ),
          ),
        ),
      ),
      body: Padding(
        padding: EdgeInsets.only(
            right: 15, left: 15, top: 15, bottom: screenHeight - 510),
        child: Container(
          padding: const EdgeInsets.all(16.0), // 保持一致性
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8.0),
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.2),
                blurRadius: 6.0,
              ),
            ],
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              TimeSwitching(
                mode: SwitchingMode.month,
                onDateChanged: _onMonthDateChanged,
              ),
              SizedBox(height: 16.0),
              Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    height: 284, // 调整背景图的高度
                    width: screenWidth - 80,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(8.0),
                    ),
                    child: Stack(
                      children: [
                        Positioned(
                          bottom: 10,
                          width: screenWidth - 80,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.center, // 垂直居中
                            children: [
                              GestureDetector(
                                onTap: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) =>
                                            const GroupRankingPage()),
                                  );
                                },
                                child: Image.asset(
                                    'assets/images/centre/top2.png'),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
